<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Field extends CI_Model {

    private $CI;
    
    function __construct() {
        parent::__construct();
    }
    
    function get_by_object($object_id) {
    	return $this->db->where('object_id',$object_id)->get('field')->result();
    }
    
	function update($object_id,$old_name,$new_name,$type) {
   		$object_name = $this->object->get($object_id)->table;

   		if ($type != 'i18n_string') {

   			$mysql_type = $this->type_mapping($type);
			$this->db->query('ALTER TABLE `'.URBAN_TABLE_PREFIX."$object_name` CHANGE COLUMN `$old_name` `$new_name` $mysql_type");
   		
   		} else {

   			$this->load->model('language');
   			$languages = $this->language->get_all();

   			foreach ($languages as $language) {
				$this->db->query('ALTER TABLE `'.URBAN_TABLE_PREFIX."$object_name` CHANGE COLUMN `$old_name".'_'.$language->id."` `$new_name".'_'.$language->id."` TEXT NOT NULL");
	
				if ($this->db->_error_number()!=0)
					throw new Exception("Error adding i18n field $object_name.$name: ".$this->db->_error_message());
   			}
   		}
		
		if ($this->db->_error_number()!=0)
			throw new Exception('Error: '.$this->db->_error_number().': '.$this->db->_error_message());
		
		
		$this
			->db
    		->where('object_id',$object_id)
    		->where('name',$old_name)
    		->update('field',array('name'=>$new_name,'type'=>$type));

    		
		if ($this->db->affected_rows() == 0)
			throw new Exception("Field $old_name not found in object #$object_id");
	}
	
	
    function delete($object_id,$name) {
   		$object_name = $this->object->get($object_id)->table;
    	
    	$type = $this
    		->db
    		->where('object_id',$object_id)
    		->where('name',$name)
    		->select('type')
    		->get('field')
    		->row()
    		->type;
    		
		if ($type != 'i18n_string') {
			$this->db->query('ALTER TABLE `'.URBAN_TABLE_PREFIX."$object_name` DROP COLUMN `$name`");
				if ($this->db->_error_number() != 0)
					throw new Exception("Error deleting field $object_name.$name: ".$this->db->_error_message());	
		} else {
			$this->load->model('language');
   			$languages = $this->language->get_all();

   			foreach ($languages as $language) {
   				$this->db->query('ALTER TABLE `'.URBAN_TABLE_PREFIX."$object_name` DROP COLUMN `$name"."_$language->id");
				if ($this->db->_error_number() != 0)
					throw new Exception("Error deleting i18n field $object_name.$name: ".$this->db->_error_message());
   			}
		}
    		
    		
    	$this
    		->db
    		->where('object_id',$object_id)
    		->where('name',$name)
    		->delete('field');

		if ($this->db->affected_rows() == 0)
			throw new Exception("Field $name not found in object #$object_id");
	}
	    
    
    function insert($object_id,$name,$type) {
   		$object_name = $this->object->get($object_id)->table;
    	
   		if ($type != 'i18n_string') {

   			$mysql_type = $this->type_mapping($type);
			$this->db->query('ALTER TABLE `'.URBAN_TABLE_PREFIX."$object_name` ADD `$name` $mysql_type");

			if ($this->db->_error_number()!=0)
				throw new Exception("Error adding field $object_name.$name: ".$this->db->_error_message());

   		} else {

   			$this->load->model('language');
   			$languages = $this->language->get_all();

   			foreach ($languages as $language) {
				$this->db->query('ALTER TABLE `'.URBAN_TABLE_PREFIX."$object_name` ADD `$name".'_'.$language->id."` TEXT NOT NULL");
				if ($this->db->_error_number()!=0)
					throw new Exception("Error adding i18n field $object_name.$name: ".$this->db->_error_message());
   			}
   		}
    		
    	$this
    		->db
    		->insert('field',array(
    			'name'=>$name,
    			'object_id'=>$object_id,
    			'type'=>$type
    		));
    	if ($this->db->affected_rows() == 0)
    		throw new Exception("Error adding field $object_name.$name");
    }
   
    private function type_mapping($type) {
    	switch($type) {
    		case 'integer':		return 'INT NOT NULL';
    		case 'decimal':		return 'DECIMAL NOT NULL';
			case 'string':		return 'TEXT NOT NULL';
			case 'color':		return 'CHAR(6) DEFAULT "000000"';
			case 'date':		return 'DATE NOT NULL';
			case 'datetime':	return 'DATETIME NOT NULL';
			case 'boolean':		return 'BOOLEAN NOT NULL';
			default:
				throw new Exception("Invalid type: $type");
    	}
    }

}?>